package org.bt.gateway;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.core.env.Environment;

import javax.annotation.PostConstruct;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Locale;

/**
 * @author zwj
 */
@SpringBootApplication
@ComponentScan(value = {
        "org.bt.gateway",
        "org.bt.common.config.satoken",
        "org.bt.common.config.redis",
        "org.bt.common.config.orika",
        "org.bt.common.utils",
        "org.bt.common.exception"})
@EnableDiscoveryClient
@Slf4j
public class GatewayServerApplication {

    public static void main(String[] args) {
        initApp(SpringApplication.run(GatewayServerApplication.class, args));
    }

    private static void initApp(ConfigurableApplicationContext applicationContext) {
        Environment env = applicationContext.getEnvironment();
        String ip = "0.0.0.0";
        try {
            ip = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            log.error(String.format("获取宿主机IP地址失败:{}", e.getMessage()));
        }
        String port = env.getProperty("server.port");
        String path = env.getProperty("server.servlet.context-path");
        if (path == null) {
            path = "";
        }
        log.info("\n----------------------------------------------------------\n\t" +
                "Application is running! Access URLs:\n\t" +
                "Local: \t\thttp://localhost:" + port + path + "/\n\t" +
                "External: \thttp://" + ip + ":" + port + path + "/\n\t" +
                "knife4j API: \thttp://" + ip + ":" + port + "/doc.html\n\t" +
                "Swagger API: \thttp://" + ip + ":" + port + "/swagger-ui/index.html\n\n" +
                "----------------------------------------------------------");
    }

    @PostConstruct
    private void setDefaultLocal() {
        LocaleContextHolder.setDefaultLocale(Locale.CHINA);
    }
}
